namespace Sckf.MicroService.Services.Catalog.Application.Catalogs;

public partial class CatalogQueryHandler
{
    [EventHandler]
    public async Task CatalogHandleAsync(CatalogQuery query)
    {
        query.Result = await _catalogRepository.SingleAsync(query.CatalogId);
    }

    [EventHandler]
    public async Task CatalogsHandleAsync(CatalogsQuery query)
    {
        var _query = _catalogRepository.Query(a => a.Name.Contains(query.Keyword));

        var totalItems = await _query.LongCountAsync();

        var itemsOnPage = await _query
            .OrderBy(item => item.Id)
            .Skip((query.PageIndex - 1) * query.PageSize)
            .Take(query.PageSize)
            .ToListAsync();

        query.Result = new PaginatedResultViewModel<Domain.Entities.Catalog>(query.PageIndex, query.PageSize, totalItems, itemsOnPage);
    }
}

public partial class CatalogQueryHandler
{
    private readonly ICatalogRepository _catalogRepository;

    public CatalogQueryHandler(ICatalogRepository catalogRepository)
        => _catalogRepository = catalogRepository;
}